﻿using MvcContrib.TestHelper;
using NUnit.Framework;
using Rhino.Mocks;
using System;
using System.Collections.Generic;
using System.Web.Mvc;


namespace ProductInfo.Core.UT.Controllers
{
	[TestFixture]
	public class ReleaseNoteEntryController
	{
		private ProductInfo.Core.Service.IReleaseNoteEntryService c_releaseNoteEntryService;
		private ProductInfo.Core.Controllers.ReleaseNoteEntryController c_releaseNoteEntryController;


		[SetUp]
		public void SetUp()
		{
			this.c_releaseNoteEntryService = MockRepository.GenerateStub<ProductInfo.Core.Service.IReleaseNoteEntryService>();
			this.c_releaseNoteEntryController = new ProductInfo.Core.Controllers.ReleaseNoteEntryController(this.c_releaseNoteEntryService);
		}

		
		[Test]
		public void List_Action_Returns_Populated_ReleaseNoteEntry()
		{
			var _releaseNoteEntryList = new List<ProductInfo.Core.Models.ReleaseNoteEntry>
         		{
         			ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good
         		};
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.List()).Return(_releaseNoteEntryList);

			var _viewResult = this.c_releaseNoteEntryController.List() as ViewResult;
			
			var _model = _viewResult.ViewData.Model as List<ProductInfo.Core.Models.ReleaseNoteEntry>;
			Assert.IsNotNull(_model, "Model is not null");
			Assert.AreEqual(_releaseNoteEntryList.Count, _model.Count, "Model count");
		}


		[Test]
		public void List_Action_Returns_Populated_ReleaseNoteEntry_USING_MVCCONTRIB_TESTHELPER()
		{
			var _releaseNoteEntryList = new List<ProductInfo.Core.Models.ReleaseNoteEntry>
         		{
         			ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good
         		};
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.List()).Return(_releaseNoteEntryList);

			var _viewResult = this.c_releaseNoteEntryController.List();

			_viewResult.AssertViewRendered().WithViewData<List<ProductInfo.Core.Models.ReleaseNoteEntry>>();
		}


		[Test]
		public void Edit_Get_Action_Returns_Populated_ReleaseNoteEntry()
		{
			var _releaseNoteEntry = ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good;
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.Edit(_releaseNoteEntry.Id)).IgnoreArguments().Return(_releaseNoteEntry);

			var _viewResult = this.c_releaseNoteEntryController.Edit(_releaseNoteEntry.Id) as ViewResult;
			
			var _model = _viewResult.ViewData.Model as ProductInfo.Core.Models.ReleaseNoteEntry;
			Assert.IsNotNull(_model, "Model is not null");
		}


		[Test]
		public void Get_Edit_Actions_Returns_Populated_ReleaseNoteEntry_USING_MVCCONTRIB_TESTHELPER()
		{
			var _releaseNoteEntry = ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good;
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.Edit(_releaseNoteEntry.Id)).IgnoreArguments().Return(_releaseNoteEntry);

			var _viewResult = this.c_releaseNoteEntryController.Edit(_releaseNoteEntry.Id);

			_viewResult.AssertViewRendered().WithViewData<ProductInfo.Core.Models.ReleaseNoteEntry>();
		}


		[Test]
		public void Edit_Post_Action_Returns_Redirect_When_Model_Valid()
		{
			var _releaseNoteEntry = ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good;
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.Update(_releaseNoteEntry)).IgnoreArguments().Return(true);

			var _viewResult = this.c_releaseNoteEntryController.Edit(_releaseNoteEntry) as RedirectToRouteResult;

			Assert.IsNotNull(_viewResult, "View is not null");
			Assert.AreEqual("ReleaseNoteEntry", _viewResult.RouteValues["controller"]);
			Assert.AreEqual("List", _viewResult.RouteValues["action"]);
		}


		[Test]
		public void Edit_Post_Action_Returns_Redirect_When_Model_Valid_USING_MVCCONTRIB_TESTHELPER()
		{
			var _releaseNoteEntry = ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good;
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.Update(_releaseNoteEntry)).IgnoreArguments().Return(true);

			var _viewResult = this.c_releaseNoteEntryController.Edit(_releaseNoteEntry) as RedirectToRouteResult;

			Assert.IsNotNull(_viewResult, "View is not null");
			_viewResult.AssertActionRedirect().ToAction<ProductInfo.Core.Controllers.ReleaseNoteEntryController>(c => c.List());
		}


		[Test]
		public void Edit_Post_Action_Returns_ViewResult_When_Model_Not_Valid()
		{
			const string _key = "Key";
			const string _errorMessage = "Error message";

			var _validationErrors = new Dictionary<string, string> {{_key, _errorMessage}};
			var _releaseNoteEntry = ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good;
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.Update(_releaseNoteEntry)).IgnoreArguments().Return(false);
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.ValidationErrors).IgnoreArguments().Return(_validationErrors);

			var _viewResult = this.c_releaseNoteEntryController.Edit(_releaseNoteEntry) as ViewResult;

			Assert.IsNotNull(_viewResult, "View is not null");
			Assert.IsNotNull(_errorMessage, _viewResult.ViewData.ModelState[_key].ToString(), "Model state error");
		}


		[Test]
		public void Edit_Post_Action_Returns_ViewResult_When_Model_Not_Valid_USING_MVCCONTRIB_TESTHELPER()
		{
			const string _key = "Key";
			const string _errorMessage = "Error message";

			var _validationErrors = new Dictionary<string, string> { { _key, _errorMessage } };
			var _releaseNoteEntry = ProductInfo.Core.UT.Factory.Models.ReleaseNoteEntry.Good;
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.Update(_releaseNoteEntry)).IgnoreArguments().Return(false);
			this.c_releaseNoteEntryService.Stub(releaseNoteEntryService => releaseNoteEntryService.ValidationErrors).IgnoreArguments().Return(_validationErrors);

			var _viewResult = this.c_releaseNoteEntryController.Edit(_releaseNoteEntry) as ViewResult;

			Assert.IsNotNull(_viewResult, "View is not null");
			_viewResult.AssertViewRendered().ViewData.ModelState.ContainsKey(_key);
		}
	}
}